﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;
using System.Text;

namespace PhoneBilling
{
    /// <summary>
    /// jquery插件中的treeview控件的帮助类，用于生成treeview控件所需要的html代码结构
    /// </summary>
    public static class JQTreeHelper
    {
        public static List<JQTreeNode> GetJsonData(DataTable dtAllNodes, string idColumnName, string textColumnName, string parentIdColumnName, string rootNodeParentIdValue)
        {
            List<JQTreeNode> rootNodes = new List<JQTreeNode>();
            DataView dvRootNodes = new DataView(dtAllNodes);
            dvRootNodes.RowFilter = parentIdColumnName + "='" + rootNodeParentIdValue + "'";
            foreach (DataRowView dv in dvRootNodes)
            {
                JQTreeNode node = new JQTreeNode();
                if (dv[idColumnName] != DBNull.Value)
                {
                    node.id = dv[idColumnName].ToString();
                }
                node.text = dv[textColumnName].ToString();
                node.children = GetJsonData(dtAllNodes, idColumnName, textColumnName, parentIdColumnName, dv[idColumnName].ToString());
                rootNodes.Add(node);
            }
            return rootNodes;
        }
        public static string GetJsonString(DataTable dtAllNodes, string idColumnName, string textColumnName, string parentIdColumnName, string rootNodeParentIdValue)
        {
            List<JQTreeNode> actual;
            actual = GetJsonData(dtAllNodes, idColumnName, textColumnName, parentIdColumnName, rootNodeParentIdValue);
            //序列化后，由于默认将所有字段都序列化输出，所以需要将没有赋值取默认值的字段替换为空，以此来减少输出的字符数
            StringBuilder result = new StringBuilder();
            new JavaScriptSerializer().Serialize(actual, result);
            result.Replace("\"id\":null,", "");
            result.Replace(",\"expanded\":false", "");
            result.Replace(",\"hasChildren\":false", "");
            result.Replace(",\"classes\":null", "");
            result.Replace(",\"children\":null", "");
            return result.ToString();
        }

        /// <summary>
        /// jquery插件中的treeview控件进行ajax请求时返回的json数据对象
        /// </summary>
        
    }

    public class JQTreeNode
    {
        /// <summary>
        /// 树节点id
        /// </summary>
        public string id { get; set; }
        /// <summary>
        /// 树节点文本
        /// </summary>
        public string text { get; set; }
        /// <summary>
        /// 是否展开
        /// </summary>
        public bool expanded { get; set; }
        /// <summary>
        /// 是否有子节点
        /// </summary>
        public bool hasChildren { get; set; }
        /// <summary>
        /// cssclass类名称
        /// </summary>
        public string classes { get; set; }
        /// <summary>
        /// 子节点集合
        /// </summary>
        public List<JQTreeNode> children { get; set; }
    }
}